/*
 * @(#)SwitchTasksOperator.java 1.0 7/12/13
 *
 */

package dominio;

import java.util.ArrayList;

/**
 * Created by Rafael Simon Garcia on 12/7/13.
 */
public class SwitchTasksOperator extends HillClimbingOperator {
    private static PermutationsIterator coordinatesPermutations;
    private static int length; //length of a tour


    //constructor with the length of a tour
    public static void InitializeOperator(int l){
        length = l;
        coordinatesPermutations = new PermutationsIterator(length);
    }

    public static boolean HasNextValue(){
        return coordinatesPermutations.HasNextValue();
    }

    public static ArrayList<Integer> Operate(ArrayList<Integer> currentPath){
        if(coordinatesPermutations == null) InitializeOperator(currentPath.size());

        ArrayList<Integer> nextPath = new ArrayList<>();

        //if there is a next value on the iterator
        if(coordinatesPermutations.HasNextValue()){
            //get that next value
            ValuesPair p = coordinatesPermutations.NextValue();
            int start= p.first, end = p.second;

            if(start != end){ //if the values from the pair are different
                nextPath = new ArrayList<>(currentPath); //copy the input tour into a new ArrayList
                SwitchPositions(nextPath, p.first, p.second);
            }
        }
        return nextPath;
    }

    public static void Reset(){ coordinatesPermutations.Reset(); }

    private static void SwitchPositions(ArrayList<Integer> tour, int i, int j){
        int first, second;

        first = tour.get(i);
        second = tour.get(j);
        tour.set(i, second);
        tour.set(j, first);
    }
}

